package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Category;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 商品类别服务实现类，实现类别管理的具体业务逻辑
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    // 创建新的商品类别
    @Override
    public Category createCategory(Category category) {
        categoryMapper.insert(category);
        return category;
    }

    // 更新商品类别信息
    @Override
    public Category updateCategory(Category category) {
        categoryMapper.updateById(category);
        return category;
    }

    // 删除指定ID的商品类别及其子类别
    @Override
    public void deleteCategory(Integer categoryId) {
        // 递归删除子类别
        List<Category> subCategories = getSubCategories(categoryId);
        for (Category subCategory : subCategories) {
            categoryMapper.deleteById(subCategory.getCategoryId());
        }
        // 删除当前类别
        categoryMapper.deleteById(categoryId);
    }

    // 获取指定ID的商品类别
    @Override
    public Category getCategoryById(Integer categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    // 获取所有商品类别
    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.selectList(null);
    }

    // 获取指定父类别的所有子类别
    @Override
    public List<Category> getSubCategories(Integer parentCategoryId) {
        return categoryMapper.findByParentCategoryId(parentCategoryId);
    }

    // 根据关键字搜索商品类别
    @Override
    public List<Category> searchCategories(String keyword) {
        return categoryMapper.findByCategoryNameContaining(keyword);
    }

    // 获取商品类别的树形结构
    @Override
    public List<Category> getCategoryTree() {
        // 获取所有类别
        List<Category> allCategories = getAllCategories();

        // 按父类别ID分组
        Map<Integer, List<Category>> categoryMap = allCategories.stream()
                .collect(Collectors.groupingBy(Category::getParentCategoryId));

        // 获取根类别（父类别ID为null的类别）
        List<Category> rootCategories = categoryMap.getOrDefault(null, new ArrayList<>());

        // 递归设置子类别
        rootCategories.forEach(category -> setSubCategories(category, categoryMap));

        return rootCategories;
    }

    // 递归设置子类别的辅助方法
    private void setSubCategories(Category category, Map<Integer, List<Category>> categoryMap) {
        List<Category> subCategories = categoryMap.getOrDefault(category.getCategoryId(), new ArrayList<>());
        category.setSubCategories(subCategories);
        subCategories.forEach(subCategory -> setSubCategories(subCategory, categoryMap));
    }
}